package com.tianqu.tms.www.transfer.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAjaxAction;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.TransferAcceptedInfo;

/**
 * @author mufeiyu
 * 
 */
public class CarPrintListInfoAjaxAction extends TmsAjaxAction{
	private Logger logger = Logger.getLogger(CarPrintListInfoAjaxAction.class);
	private static final String SERIAL = "serial";
	@Override
	public void process(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws TmsException {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();
		controller.begin();
		String serial = request.getParameter(SERIAL);

		TransferAcceptedInfo transferInfo = TransferAcceptedInfo.getInstance();
		Codes code = Codes.getInstance();
		//车架号信息
		Car car = Car.getInstance();
		car = car.findCarBySerial(serial);

		//号牌种类
		List licensePlateTypeList = new ArrayList();
		licensePlateTypeList = code.getCodeslistByItemId(Codes.LICENSEPLATE_TYPE);
		
		//业务类型
		List businessTypeList = new ArrayList();
		for(int i=1;i<3;i++){
			Map mapInfo = new HashMap();
			if(i==1){
				mapInfo.put("itemCode", i);
				mapInfo.put("itemValue", "二手车过户");
			}else if(i==2){
				mapInfo.put("itemCode", i);
				mapInfo.put("itemValue", "新车/转入上牌");
			}
			businessTypeList.add(mapInfo);
		}
		
		//拼select
		StringBuffer str = new StringBuffer();

		str.append(car.getVin());		
		str.append("##");
		
		str.append(car.getLicensePlate());
		str.append("##");
		
		Map map = null;		
		for(int i=0;i<licensePlateTypeList.size();i++)
		{
			map =(Map)licensePlateTypeList.get(i);
			//设置默认参数
			map.get("itemCode");
			if("1".equals(map.get("itemCode").toString())){
				str.append("<option value='");
				str.append(map.get("itemCode"));
				str.append("'");
				str.append(" selected='");
				str.append("true'");
				str.append(" >");
				str.append(map.get("itemValue"));
				str.append("</option>");
			
			}else{
				str.append("<option value=\""+map.get("itemCode")+"\">"+map.get("itemValue")+"</option>");
			}
		}
		str.append("##");

		for(int i=0;i<businessTypeList.size();i++)
		{
			map =(Map)businessTypeList.get(i);
			//设置默认参数
			map.get("itemCode");
			if("1".equals(map.get("itemCode").toString())){
				str.append("<option value='");
				str.append(map.get("itemCode"));
				str.append("'");
				str.append(" selected='");
				str.append("true'");
				str.append(" >");
				str.append(map.get("itemValue"));
				str.append("</option>");
			
			}else{
				str.append("<option value=\""+map.get("itemCode")+"\">"+map.get("itemValue")+"</option>");
			}
		}
		str.append("##");
		transferInfo = transferInfo.findPrintNumInfoByBasicCarId(serial);
		if(transferInfo.isExistTransferInfoByBasicCarId(serial)&&
				(transferInfo.getCy_printNum() == 0 && transferInfo.getLc_printNum() == 0)){
			str.append("1");
		}else {
			str.append("0");
		}
		
		String responseText =str.toString();
		controller.commit();
		responseValues(response, responseText, TEXT);
		
		logger.debug(this.getClass().getName() + "#doAction was completed.");
	}

}
